package com.example.mobile2030606150cjk0417;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private TextView tvMain1;
    private TextView tvMain2;
    private TextView textView3;
    private Button btnMainclose;
    private Button btnMainsubmit;
    private String[] str1 = new String[]{"轿车引擎", "轿车前玻璃", "轿车方向盘", "轿车方向盘", "轿车方向盘", "轿车方向盘"};
    private ListView listView;
    private TextView textView4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        btnMainsubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] str2 = {"新星汽车配件", "枫叶汽车配件", "天辉汽车配件", "细末汽车配件"};
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请选择供应商")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setSingleChoiceItems(str2, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                textView4.setText(str2[which]);
                            }
                        })
                        .create().show();
                AlertDialog dialog;
                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("请选择商品");
                builder.setMultiChoiceItems(str1, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        textView3.setText(null);
                        for (int i=0;i<str1.length;i++){
                            if (listView.getCheckedItemPositions().get(i)) {
                                textView3.append(str1[i]+"\n");
                            }
                        }
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });


                btnMainclose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog dialog;
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setMessage("是否确认退出");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                MainActivity.this.finish();
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.create().show();
                    }
                });


                dialog = builder.create();
                listView = dialog.getListView();
                dialog.show();
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        tvMain1 = (TextView) findViewById(R.id.tvMain1);
        tvMain2 = (TextView) findViewById(R.id.tvMain2);
        textView3 = (TextView) findViewById(R.id.textView3);
        btnMainclose = (Button) findViewById(R.id.btnMainclose);
        btnMainsubmit = (Button) findViewById(R.id.btnMainsubmit);
        textView4 = (TextView) findViewById(R.id.textView4);
    }
}